<?php
/**
 * XmuSlh, web platform of Student Association Federation of Xiamen University
 * 
 * Rewritting one or some pages of StatusNet. 
 * To get the original version, please visit <http://status.net/>.
 * 
 * StatusNet, the distributed open-source microblogging tool
 * 
 * LICENCE: This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * Get a data via this action and update the download count.
 * 
 * @author		ytukie <ytukie@gmail.com>
 * @version		0.1
 */

// XmuSlh only.
if ( !defined('XMUSLH')) { exit(1); }

require_once INSTALLDIR . '/lib/slh_action.php';
require_once INSTALLDIR . '/lib/slh_util.php';

require_once INSTALLDIR . '/classes/File.php';

class GetdataAction extends Action
{
	/**
	 * The Data object.
	 */
	var $data = null;
	
	function prepare($args)
	{
		parent::prepare($args);

		// Check the logging and rights.
		
		// Parameter from 'get' url
		$gdata_id = $this->getTrimmedArg('gdata_id');
		
		if ( !is_numeric($gdata_id)) {
			return false;
		}
		
		$this->data = Data::staticGet('id', $gdata_id);
		if (empty($this->data)) {
			$this->clientError('Cannot find the data.');
		}
		
		return true;
	}
	
	function handle($args)
	{
// FIXME: To simplify the procedure, not necessary to get the total object.
		$fileurl = $this->data->getFileUrl();
		
		if ($fileurl) {
			// Validated.
			// Add the 'downloadcnt' at 'data'.
			$this->data->increaseDownloadCount();			
			
			slh_redirect($fileurl);
		} else {
			$this->clientError('Url of the downloading file error.');
		}
	}
}

?>